//
//  AppConfigManager.m
//  ebook
//
//  Created by ChenNan on 16/5/31.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "AppConfigManager.h"



@interface AppConfigManager ()

@property (nonatomic, strong) NSDictionary *rootDic;

@end


@implementation AppConfigManager

+ (id)readAppConfigType:(NSString *)type key:(NSString *)key {
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:kAppConfigPlist ofType:@"plist"];
    NSDictionary *rootDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    
    if ([type isEqualToString:@"Color"]) {//color
        NSDictionary *colorDic = [rootDic objectForKey:type];
        NSString *colorStr = [colorDic objectForKey:key];
        NSArray *colorsArray = [colorStr componentsSeparatedByString:@","];
        if (colorsArray.count == 4) {
            return  MTColor([colorsArray[0] floatValue], [colorsArray[1] floatValue], [colorsArray[2] floatValue], [colorsArray[3] floatValue]);
        } else {
            //不存在，返回黑色
            MTLog(@"plist中不存在键对应的值，无法取得颜色值");
            return MTColor(0, 0, 0, 1.0);
        }
    } else {//其他
      
    }
    return nil;
}

+ (instancetype)shareManager {
    
    static AppConfigManager *man = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        man = [[AppConfigManager alloc] init];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:kAppConfigPlist ofType:@"plist"];
        man.rootDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    });
    return man;
}

- (UIColor *)colorWithKey:(NSString *)key {
    
    NSDictionary *colorDic = [_rootDic objectForKey:@"Color"];
    NSString *colorStr = [colorDic objectForKey:key];
    NSArray *colorsArray = [colorStr componentsSeparatedByString:@","];
    if (colorsArray.count == 4) {
        return  MTColor([colorsArray[0] floatValue], [colorsArray[1] floatValue], [colorsArray[2] floatValue], [colorsArray[3] floatValue]);
    } else {
        //不存在，返回黑色
        MTLog(@"###warn! plist中不存在键对应的值，无法取得颜色值:%@",key);
        return MTColor(0, 0, 0, 1.0);
    }
}

- (UIImage *)imageWithKey:(NSString *)key {
    
    NSDictionary *imageDic = [_rootDic objectForKey:@"Image"];
    NSString *imageName = [imageDic objectForKey:key];
    if (imageName.length>0) {
        return  [UIImage imageNamed:imageName];
    } else {
        //不存在，返回黑色
        MTLog(@"###warn! plist中不存在键对应的值，无法取得图片:%@",key);
        return nil;
    }
}

- (NSInteger)integerWithKey:(NSString *)key{
   
    NSDictionary *intDic = [_rootDic objectForKey:@"Integer"];
    NSString *intStr = [intDic objectForKey:key];
    if(!intStr)MTLog(@"###warn! plist中不存在键对应的值，无法取得Int:%@",key);
    return [intStr integerValue];
}

- (NSArray *)FontsData {
    
    return [_rootDic objectForKey:@"Fonts"];
}

@end
